package com.paomi.client.view;

import android.content.Context;
import android.graphics.Color;
import android.text.Html;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.paomi.client.R;
import com.paomi.client.activity.reply.model.ReplyContent;

import java.util.List;


/**
 * Created by ${Adrian} on 17/5/5.
 */

public class CommentTextView extends LinearLayout {
    Context context;
    private TextView tv_content1, tv_content2, tv_more;

    public CommentTextView(Context context) {
        super(context);
        this.context = context;

    }

    public CommentTextView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        LayoutInflater.from(context).inflate(R.layout.view_comment, this);
        tv_content1 = (TextView) findViewById(R.id.tv_content1);
        tv_content2 = (TextView) findViewById(R.id.tv_content2);
        tv_more = (TextView) findViewById(R.id.tv_more);

    }


    public void setComment(List<ReplyContent> replys, int reply_num) {

        int length = replys.size();
        if (length == 1) {
//            int pos_name = comment.replies.get(0).getUserName().length();
            int pos_name = replys.get(0).getUserName().length();
            SpannableString styledText = new SpannableString(replys.get(0).getUserName() + ": " + replys.get(0).getContent());
            styledText.setSpan(new ForegroundColorSpan(Color.parseColor("#333333")), 0, pos_name, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            styledText.setSpan(new ForegroundColorSpan(Color.parseColor("#999999")), pos_name + 1, styledText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

            tv_content1.setVisibility(View.VISIBLE);
//                    holder.tv_content1.setText(comment.replies.get(0).getContent());
            tv_content1.setText(styledText, TextView.BufferType.SPANNABLE);
            tv_content2.setVisibility(GONE);
            tv_more.setVisibility(GONE);
        }

        if (length == 2) {
            int pos_name = replys.get(length - 1).getUserName().length();
            SpannableString styledText = new SpannableString(replys.get(length - 1).getUserName() + ": " + replys.get(length - 1).getContent());
            styledText.setSpan(new ForegroundColorSpan(Color.parseColor("#333333")), 0, pos_name, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            styledText.setSpan(new ForegroundColorSpan(Color.parseColor("#999999")), pos_name + 1, styledText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            tv_content1.setVisibility(View.VISIBLE);
//                    holder.tv_content1.setText(comment.replies.get(0).getContent());
            tv_content1.setText(styledText, TextView.BufferType.SPANNABLE);
            tv_content2.setVisibility(GONE);
            tv_more.setVisibility(GONE);

            int posname = replys.get(length - 2).getUserName().length();
            if (replys.get(length - 2).getTo_name() == null) {
                SpannableString st = new SpannableString(replys.get(length - 2).getUserName() + ": " + replys.get(length - 2).getContent());
                st.setSpan(new ForegroundColorSpan(Color.parseColor("#333333")), 0, posname, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                st.setSpan(new ForegroundColorSpan(Color.parseColor("#999999")), posname + 1, st.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                tv_content2.setVisibility(View.VISIBLE);
//                    holder.tv_content1.setText(comment.replies.get(0).getContent());
                tv_content2.setText(st, TextView.BufferType.SPANNABLE);
                tv_more.setVisibility(GONE);
            } else {
                SpannableStringBuilder ss = new SpannableStringBuilder(replys.get(length - 2).getUserName() + " 回复 " + replys.get(length - 2).getTo_name() + ": " + replys.get(length - 2).getContent());
//                ss.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.title_text)),pos_name,pos_name+2,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                ss.setSpan(new ForegroundColorSpan(Color.parseColor("#333333")), 0, pos_name-1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                ss.setSpan(new ForegroundColorSpan(Color.parseColor("#999999")), posname, posname + 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                ss.setSpan(new ForegroundColorSpan(Color.parseColor("#333333")), posname + 3, posname + 4 + replys.get(length - 2).getTo_name().length()-1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                ss.setSpan(new ForegroundColorSpan(Color.parseColor("#999999")), posname + 4 + replys.get(length - 2).getTo_name().length(), ss.length()
                        , Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                tv_content2.setVisibility(VISIBLE);
                tv_content2.setText(ss, TextView.BufferType.SPANNABLE);
                tv_more.setVisibility(GONE);
            }
        }
        if (length > 2) {
            tv_more.setVisibility(VISIBLE);
            String text = "查看全部<font color='#e72e62'>" + reply_num + "</font>条回复";
            tv_more.setText(Html.fromHtml(text));
            int pos_name = replys.get(length - 1).getUserName().length();
            SpannableString styledText = new SpannableString(replys.get(length - 1).getUserName() + ": " + replys.get(length - 1).getContent());
            styledText.setSpan(new ForegroundColorSpan(Color.parseColor("#333333")), 0, pos_name, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            styledText.setSpan(new ForegroundColorSpan(Color.parseColor("#999999")), pos_name + 1, styledText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            tv_content1.setVisibility(View.VISIBLE);
//                    holder.tv_content1.setText(comment.replies.get(0).getContent());
            tv_content1.setText(styledText, TextView.BufferType.SPANNABLE);

            int posname = replys.get(length - 2).getUserName().length();
            if (replys.get(length - 2).getTo_name() == null) {

                SpannableString st = new SpannableString(replys.get(length - 2).getUserName() + ": " + replys.get(length - 2).getContent());
                st.setSpan(new ForegroundColorSpan(Color.parseColor("#333333")), 0, posname, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                st.setSpan(new ForegroundColorSpan(Color.parseColor("#999999")), posname + 1, st.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                tv_content2.setVisibility(View.VISIBLE);
//                    holder.tv_content1.setText(comment.replies.get(0).getContent());
                tv_content2.setText(st, TextView.BufferType.SPANNABLE);
            } else {
                SpannableStringBuilder ss = new SpannableStringBuilder(replys.get(length - 2).getUserName() + "回复" + replys.get(length - 2).getTo_name() + ": " + replys.get(length - 2).getContent());
//                        ss.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.title_text)),pos_name,pos_name+2,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                ss.setSpan(new ForegroundColorSpan(Color.parseColor("#333333")), 0, pos_name, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                ss.setSpan(new ForegroundColorSpan(Color.parseColor("#999999")), posname, posname + 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                ss.setSpan(new ForegroundColorSpan(Color.parseColor("#333333")), posname + 2, posname + 4 + replys.get(length - 2).getTo_name().length()-1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                ss.setSpan(new ForegroundColorSpan(Color.parseColor("#999999")), posname + 4 + replys.get(length - 2).getTo_name().length(), ss.length()
                        , Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                tv_content2.setVisibility(VISIBLE);
                tv_content2.setText(ss, TextView.BufferType.SPANNABLE);
            }


        }
    }
}
